/*
 * @Description:
 * @Author: 龙春雨
 * @Date: 2023-04-13 16:21:13
 */
import { isValidateNoneMobile, isEmail } from '@/util/validate';
import { checkPwdStren } from '@/util';
import { PWD_STRENGTH } from '@/const/type';
import { validatenull } from '@/util/validate';
const validatePhone = (rule, value, callback) => {
  if (isValidateNoneMobile(value)[0]) {
    callback(new Error(isValidateNoneMobile(value)[1]));
  } else {
    callback();
  }
};
// const validatePass = (rule, value, callback) => {
//   if (this.ruleForm.password !== '') {
//     if (value !== this.ruleForm.newpassword1) {
//       callback(new Error('两次输入密码不一致!'));
//     } else {
//       callback();
//     }
//   } else {
//     callback();
//   }
// };

const validateEmail = (rule, value, callback) => {
  if (value !== '') {
    callback();
    return;
  }
  if (!isEmail(value)) {
    callback(new Error('请输入正确的邮箱地址！'));
  } else {
    callback();
  }
};
export const formOption = (e) => {
  return {
    border: true,
    align: 'center',
    menuAlign: 'center',
    span: 10,
    column: [
      {
        label: '头像',
        prop: 'avatar',
        type: 'upload',
        listType: 'picture-img',
        fileSize: 5 * 1024,
        fileType: 'img',
        row: true,
        propsHttp: {
          name: 'hash',
          url: 'key',
        },
        oss: 'qiniu',
        cropperOption: {
          autoCropWidth: 100,
          autoCropHeight: 100,
          fixed: true,
          fixedNumber: [1, 1],
        },
        rules: [
          {
            required: true,
            message: '请上传头像',
            trigger: 'change',
          },
        ],
        tip: '请上传jpeg/png/jpeg类型的文件，最大可上传5M',
      },
      {
        label: '用户名',
        prop: 'username',
        disabled: true,
        row: true,
      },
      {
        label: '手机号',
        prop: 'phone',
        row: true,
        rules: [{ required: false, validator: validatePhone, trigger: 'blur' }],
      },
      {
        label: '昵称',
        prop: 'nickname',
        maxlength: 20,
        row: true,
      },
      {
        label: '姓名',
        prop: 'name',
        maxlength: 20,
        row: true,
      },
      {
        label: '邮箱',
        prop: 'email',
        rules: [{ required: false, validator: validateEmail, trigger: 'blur' }],
        row: true,
      },
      {
        label: '原密码',
        prop: 'password',
        rules: [
          {
            required: true,
            min: 6,
            message: '原密码不少于6位',
            trigger: 'blur',
          },
        ],
        row: true,
      },
      {
        label: '新密码',
        prop: 'newpassword1',
        component: 'PwdTipIndex',
        maxlength: 20,
        rules: [
          {
            required: false,
            min: 6,
            message: '新密码不少于6位',
            trigger: 'blur',
          },
          {
            required: false,
            validator: (rule, value, callback) => {
              if (value !== '') {
                const pwdLen = checkPwdStren(value);
                if (validatenull(value)) {
                  callback(new Error('请输入新密码'));
                } else if (pwdLen < PWD_STRENGTH.STRONG) {
                  // 请输入6位以上强度为强的密码！
                  callback(new Error('请输入6位以上强度为强的密码！'));
                } else {
                  callback();
                }
              }
            },
            trigger: 'blur',
          },
        ],
        row: true,
      },
      {
        label: '确认密码',
        prop: 'newpassword2',
        component: 'PwdTipIndex',
        maxlength: 20,
        rules: [
          {
            required: false,
            validator: (rule, value, callback) => {
              // console.log(
              //   13,
              //   e.$children[0].$children[0].$children[0].$refs[
              //     'newpassword2'
              //   ][0].$children[0].$refs.pwdTip.isValid()
              // );
              if (e.ruleForm.password !== '') {
                if (value !== e.ruleForm.newpassword1) {
                  callback(new Error('两次输入密码不一致!'));
                } else {
                  callback();
                }
              } else {
                callback();
              }
            },
            trigger: 'blur',
          },
        ],
        row: true,
      },
    ],
  };
};
